/**
 * Created with IntelliJ IDEA.
 * User: john
 * Date: 13-7-29
 * Time: 上午11:30
 * To change this template use File | Settings | File Templates.
 */

$(document).ready(function(){
    ajaxUserData();

    $("#imgFrame").load(function () {
        var $targetImg = $("#imgFrame").contents().find("img#photo");
        var imgId = $targetImg.attr("imgId");
        if (imgId) {
            $("#myModal").modal("hide");
        } else {
            var imgSrc = $("#uphoto").attr("src");
            $targetImg.attr("src", imgSrc);
        }
        var userName = $("#imgFrame").contents().find("#userName");
        userName.html($("#uname").html());
    });
});

function ajaxUserData(){
    $.ajax({
        type: "GET",
        url: window.config.url_user + "/info",
        dataType:"json",
        success: function(msg){
            var PName = msg.PName;
            if(PName==""){ PName = "未设置"; }
            $("#PName").html(PName);
            var groupName = msg.groupName;
            if(groupName==""){ groupName = "未设置"; }
            $("#groupName").html(groupName);
            var jobNum = msg.jobNum;
            if(jobNum==""){ jobNum = "未设置"; }
            $("#ujobNum").html(jobNum);
            var linkType = msg.linkType;
            if(linkType==""){ linkType = "未设置"; }
            $("#ulinktype").html(linkType);
            var nickName = msg.nickName;
            if(nickName==""){ nickName = "未设置"; }
            $("#uname").html(nickName);
            $("#uemail").html(msg.email);
            $("#uctime").html(msg.cTime);
            var icon = msg.icon;
            $("#id_uphoto").val(icon);
            var show_icon = icon && icon != '' ? (_config.url_file + '/show/' + icon) : '../res/images/u_person-photo.jpg';
            $("#uphoto").attr("src",show_icon);
        }
    });
}


function showEdit(showId){
    if(showId!="uphoto"){
        var inputValue = $("#id_"+showId).val();
        if(inputValue!=undefined){
            $("#"+showId).html(inputValue);
        }else{
            var showValue = $("#"+showId).html();
            $("#"+showId).html("<input type='text' style='background-color:#cbcdd4;border:1px solid #9da2ad;' id='id_"+showId+"' name='"+showId+"'/>");
            $("#id_"+showId).val(showValue);
        }
    }else{
        $("#myModal").modal("show");
    }
}

$("#myModal").on("hidden", function () {
    var $targetImg = $("#imgFrame").contents().find("img#photo");
    var imgSrc = $targetImg.attr("src");
    if (imgSrc) {
        var imgId = $targetImg.attr("imgId");
        if(imgId!=""){
            $("#uphoto").attr("src", imgSrc);
            $("#id_uphoto").val(imgId);
        }
    }
    var src = "/upload/index.jsp?" + new Date().getTime();
    $("#imgFrame").attr("src", src);
});



$("#myModal").on("shown", function () {
    var imgSrc = $("#contactPhoto").attr("src");
    if (imgSrc) {
        $("#imgFrame").contents().find("img#photo").attr("src", imgSrc);
    }
});

function submitEdit(){
    var uLinkType = $("#id_ulinktype").val()==undefined?"":$("#id_ulinktype").val();
    if(uLinkType!=""){
        if(!/^[0-9]*$/.test(uLinkType) || uLinkType.length!=11){
            $("#id_ulinktype").focus();
            alert("手机号码必须输入11位数字!");
            return;
        }
    }
    var uEmail = $("#id_uemail").val()==undefined?"":$("#id_uemail").val();
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    if(uEmail!=""){
        if (!reg.test(uEmail)) {
            $("#id_uemail").focus();
            alert("邮箱地址输入不正确!");
            return;
        }
    }
    var data = "{cid:'123'";
    if(uLinkType!=""){ data+= (",uLinkType:'"+uLinkType+"'");}
    if(uEmail!=""){ data+= (",uEmail:'"+uEmail+"'");}
    var uPhoto = $("#id_uphoto").val()==undefined?"":$("#id_uphoto").val();
    if(uPhoto!=""){ data+= (",uPhoto:'"+uPhoto+"'");}
    data += "}";
    $.ajax({
        type: "POST",
        url: window.config.url_user + "/updateInfo",
        data: data,
        dataType:"json",
        success: function(msg){
            var state = msg.state;
            if(state == "1"){
               ajaxUserData();
               window.parent.getUserInfo();
               alert("信息修改成功!");
            }else{
               alert("数据修改失败");
            }
        }
    });
}

